﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SGArchiveSystem7xDB;
using SGArchiveSystem7xDB.Linq;

namespace SGArchiveSystem7x.Models
{
    /// <summary>
    /// 菜单显示在树里的节点
    /// </summary>
    public class CDepartmentTreeModel
    {
        /// <summary>
        /// 菜单
        /// </summary>
        public T_Deparetment Deparetment;
        /// <summary>
        /// 菜单下的子菜单
        /// </summary>
        public List<T_Deparetment> childDepartment;

        /// <summary>
        /// 建立菜单
        /// </summary>
        /// <returns></returns>
        public static List<CDepartmentTreeModel> CreateModel()
        {
            List<CDepartmentTreeModel> list = new List<CDepartmentTreeModel>();
            int count = 0;
            var ms = T_DeparetmentDal.Get_Department(out count);
            ///数据出来是排序的，PID为0的排序在最前面，所以一次for循环即可完成
            for (int i = 0; i < ms.Count; i++)
            {
                if (ms[i].Dep_PID == 0)
                {
                    CDepartmentTreeModel m = new CDepartmentTreeModel();
                    m.Deparetment = ms[i];
                    m.childDepartment = new List<T_Deparetment>();
                    list.Add(m);
                }
                else
                {
                    foreach (var s in list)
                    {
                        if (s.Deparetment.Dep_ID == ms[i].Dep_PID)
                        {
                            s.childDepartment.Add(ms[i]);
                            break;
                        }
                    }
                }
            }
            return list;
        }
    }
}